<template>
	<view class="home bg">
		<uni-status-bar />
		<view class="card card-user">
			<view class="btn" @tap="openSet">
				<image class="icon" src="@/static/icon/icon_set.png" mode='aspectFill'></image>
			</view>
			<view class="card-header" @tap="previewAndChangeImage(avatar)">
				<image class="icon" :src="avatar" mode='aspectFill'></image>
			</view>
			<view class="card-body">{{ userName }}</view>
		</view>
		
		<view class="navbar">
			<view class="navbar-group">
				<view class="navbar-item" v-for="(nav, index) in navs" :key="index" @tap="openWin(nav.url)">
					<view class="navbar-item-header">
						<image class="icon" :src="nav.icon" mode="aspectFit"></image>
					</view>
					<view class="navbar-item-body">{{ nav.label }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { queryNearbyMedia, getMediaDetailByMap, uploadPic, queryUserInfo, uploadHead} from '@/api/configure.js';
	import { deepClone, getToken, toLogin, previewImage, chooseImage} from '@/util/util.js';
	import configUrl from '@/util/url.js';
	export default {
		data() {
			return {
				userName: '',
				phonenumber: '',
				avatar: '',
				navs: [{
					icon: require('@/static/icon/icon_media.png'),
					label: '媒体巡检',
					url: '/pages/media/map'
				}, {
					icon: require('@/static/icon/icon_query.png'),
					label: '媒体查询',
					url: '/pages/media/index'
				},{
					icon: require('@/static/icon/icon_record.png'),
					label: '巡检记录',
					url: '/pages/case/list'
				}]
			}
		},
		methods: {
			openSet() {
				uni.navigateTo({
					url: '/pages/index/set'
				});
			},
			openWin(url) {
				uni.navigateTo({
					url: url
				});
			},
			async changeHead() {
				let obj = {
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera']
				}
				
				let { tempFilePaths: imgsFile} = await chooseImage(obj);
				
				let data = {
					tempFilePath: imgsFile[0]
				}
				
				let res = await uploadHead(data);
			},
			async getUserInfo() {
				await queryUserInfo().then((res) => {
					this.userName = res.user.nickName;
					this.phonenumber = res.user.phonenumber;
					this.avatar = `${configUrl.CASEIMAGE_URL}${res.user.avatar.substring(1)}`;
				})
			},
			previewImage(url) {
				let _this = this;
				
				return new Promise((resolve, reject) => {
					let urls = [url];
					let obj = {
						current: 0,
						urls: urls,
						longPressActions: {
							itemList: ['更换头像'],
							success: (data) => {
								resolve(data);
								
							},
							fail: () => {
								reject();
							}
						}
					}
					
					uni.previewImage(obj);
				})
			},
			async previewAndChangeImage(url){
				let { tapIndex } = await this.previewImage(url);
				
				if(tapIndex == 0){
					await this.changeHead();
				}
				
				await this.getUserInfo();
			},
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			async init() {
				await this.getUserInfo();
			}
		},
		onLoad() {
			let token = getToken();
			
			if (!token) {
				toLogin();
			} else {
				this.init();
			}
			
		},
		onReady() {}
	}
</script>

<style lang="scss">
.home{
	background: #eff3f9 url('~@/static/icon/login_bg2.png') 50% 0% no-repeat;
	background-size: 100% auto;
}

.card{
	&.card-user{
		padding: 160rpx 0rpx 60rpx;
		margin: 0rpx auto;
		position: relative;
		top: 0rpx;
		left: 0rpx;
		
		.card-header{
			width: 215rpx;
			height: 215rpx;
			box-sizing: border-box;
			margin: 0rpx auto 17rpx;
			background: url('~@/static/icon/user_bg.png') 0% 0% repeat;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
			.icon{
				width: 188rpx;
				height: 188rpx;
				border-radius: 50%;
			}
		}
		.card-body{
			width: 269rpx;
			height: 58rpx;
			margin: 0rpx auto;
			padding: 6rpx 0rpx; 
			font-size: 38rpx;
			line-height: 46rpx;
			color: #ffffff;
			text-align: center;
			background: url('~@/static/icon/user_bg.png') 0% 0% repeat;
			border-radius: 22rpx;
			box-sizing:border-box;
		}
		
		.btn{
			width: 48rpx;
			height: 48rpx;
			position: absolute;
			top: 70rpx;
			right: 27rpx;
			.icon{
				width: 100%;
				height: 100%;
			}
		}
	}
}

.navbar{
	height: 744rpx;
	background-color: #ffffff;
	margin: 0rpx 40rpx;
	padding: 105rpx 30rpx 0rpx;
	border-radius: 38rpx;
	box-sizing: border-box;
	.navbar-group{
		.navbar-item{
			display: flex;
			padding: 25rpx 27rpx 25rpx 27rpx;
			margin-bottom: 47rpx;
			border: 1rpx solid #e1e1e1;
			border-radius: 10rpx;
			align-items: center;
			.navbar-item-header{
				width: 43rpx;
				height: 45rpx;
				.icon{
					width: 43rpx;
					height: 45rpx;
				}
			}
			.navbar-item-body{
				width: calc(100% - 43rpx);
				padding: 3rpx 21rpx 3rpx 20rpx;
				font-size: 32rpx;
				line-height: 39rpx;
			}
		}
	}
}
</style>
